from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from module_admin.dao.kq_log_data_dao import Kq_log_dataDao
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.entity.vo.kq_log_data_vo import Kq_log_dataPageQueryModel, Kq_log_dataModel, DeleteKq_log_dataModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class Kq_log_dataService:
    """
    考勤原始数据模块服务层
    """

    @classmethod
    async def get_kq_log_data_list_services(
        cls, query_db: AsyncSession, query_object: Kq_log_dataPageQueryModel, is_page: bool = False
    ):
        """
        获取考勤原始数据列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 考勤原始数据列表信息对象
        """
        kq_log_data_list_result = await Kq_log_dataDao.get_kq_log_data_list(query_db, query_object, is_page)

        return kq_log_data_list_result


    @classmethod
    async def add_kq_log_data_services(cls, query_db: AsyncSession, page_object: Kq_log_dataModel):
        """
        新增考勤原始数据信息service

        :param query_db: orm对象
        :param page_object: 新增考勤原始数据对象
        :return: 新增考勤原始数据校验结果
        """
        try:
            await Kq_log_dataDao.add_kq_log_data_dao(query_db, page_object)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message='新增成功')
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def edit_kq_log_data_services(cls, query_db: AsyncSession, page_object: Kq_log_dataModel):
        """
        编辑考勤原始数据信息service

        :param query_db: orm对象
        :param page_object: 编辑考勤原始数据对象
        :return: 编辑考勤原始数据校验结果
        """
        edit_kq_log_data = page_object.model_dump(exclude_unset=True, exclude={'del_flag', 'create_by', 'create_time', })
        kq_log_data_info = await cls.kq_log_data_detail_services(query_db, page_object.id)
        if kq_log_data_info.id:
            try:
                await Kq_log_dataDao.edit_kq_log_data_dao(query_db, edit_kq_log_data)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='考勤原始数据不存在')

    @classmethod
    async def delete_kq_log_data_services(cls, query_db: AsyncSession, page_object: DeleteKq_log_dataModel):
        """
        删除考勤原始数据信息service

        :param query_db: orm对象
        :param page_object: 删除考勤原始数据对象
        :return: 删除考勤原始数据校验结果
        """
        if page_object.ids:
            id_list = page_object.ids.split(',')
            try:
                for id in id_list:
                    await Kq_log_dataDao.delete_kq_log_data_dao(query_db, Kq_log_dataModel(id=id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入序号为空')

    @classmethod
    async def kq_log_data_detail_services(cls, query_db: AsyncSession, id: int):
        """
        获取考勤原始数据详细信息service

        :param query_db: orm对象
        :param id: 序号
        :return: 序号对应的信息
        """
        kq_log_data = await Kq_log_dataDao.get_kq_log_data_detail_by_id(query_db, id=id)
        if kq_log_data:
            result = Kq_log_dataModel(**CamelCaseUtil.transform_result(kq_log_data))
        else:
            result = Kq_log_dataModel(**dict())

        return result

    @staticmethod
    async def export_kq_log_data_list_services(kq_log_data_list: List):
        """
        导出考勤原始数据信息service

        :param kq_log_data_list: 考勤原始数据信息列表
        :return: 考勤原始数据信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'id': '序号',
            'userId': '用户编号',
            'userName': '用户姓名',
            'amPm': '考勤时段',
            'khWork': '考勤类型',
            'punchDate': '打卡日期',
            'punchTime': '打卡时间',
            'punchResult': '考勤结果',
            # 'delFlag': '删除标志',
            # 'createBy': '创建者',
            # 'createTime': '创建时间',
            # 'updateBy': '更新者',
            # 'updateTime': '更新时间',
            # 'remark': '备注',
        }
        binary_data = ExcelUtil.export_list2excel(kq_log_data_list, mapping_dict)

        return binary_data
